var flag = 0;
var pageNo = 1;
var pageSize = 10;
var check = -1;
var businessCategoryId = "";
var airportTerminalGateId="";
var airports = "";
showList();
/////店铺类型
$("document").ready(function () {
    ajax({
        url: "business/queryBusinessCategory",
        data: {},
        success: function (data) {
            
            var html = template("styleTemplate", data);
            document.getElementById('shopType').innerHTML = html;
        }
    });
});

//取消选中
$("#cancelConnect").click(function(){
    $('.i-checks').iCheck('uncheck');
});

//关联店铺
$("#submit").click(function () {
    chooseShops();
});

//按键颜色
$(".btnStyle").click(function(){
    $(".btnStyle").removeClass("btnColor");
    $(this).addClass("btnColor");
});

function chooseShops() {
    var checked=$('div.iradio_square-green.checked')[0];
    if(!checked){
        delete window.localStorage.shopRadioConnect;
    }else{
        var td=checked.parentNode.parentNode;
        var biid=$(td).attr("data-biid");
        var biName=$(td).attr("data-biName");
        var shopRadioConnect={"biid":biid,"biName":biName};
        setStorJson("shopRadioConnect",shopRadioConnect);
    }
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
}
/////table的ajax请求
function showList() {
    if (flag !== 0) {
        return;
    }
    flag = 1;
    if (getStorJson("airportConnect") != null) {
        airports = getStorJson("airportConnect").aiId
    }
    ajax({
        url: "business/queryBusinessData",
        data: {
            //多个机场id
            airports: airports,
            //店铺名称
            biName: "",
            //店铺编号
            bNum: "",
            //店铺人员
            userName: "",
            //服务电话
            contactNum: "",
            //登机口
            airportTerminalGateId: airportTerminalGateId,
            //店铺类型
            businessCategoryId: businessCategoryId,
            //第几页
            pageNo: pageNo,
            //页面大小
            pageSize: pageSize,
            //请求状态
            requestStatus: check
        },
        success: function (data) {
            
            flag = 0;
            temp(data);
            createPageInfo(data.data.queryBusinessData);
        }
    });
}


//请求成功后执行的table列表
function temp(data) {
    var count = data.data.queryBusinessAmount;
    $('#total').html(count.total);
    $('#signed').html(count.signed);
    $('#noSign').html(count.noSign);
    var shopData = data.data.queryBusinessData;
    var html = template("template", shopData);
    document.getElementById('tbody').innerHTML = html;
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
    if (getStorJson("shopRadioConnect") != null) {
        $(".radio").each(function (i, d) {
            if($(d).attr("data-biid")==getStorJson("shopRadioConnect").biid){
                $(this).iCheck('check')
            }
        })
    }

}

//选择店铺类型
$('#shopType').change(function () {
    businessCategoryId = $(this).val();
    pageNo=1;
    showList();
});
//选择登机口
$('#boardingGate').change(function () {
    airportTerminalGateId=$(this).val();
    pageNo=1;
    showList();
});


//改变页面显示数量
$("#pageSizeInfo").change(function () {
    $("#pageSizeInfo").blur();
    if (flag == 1) {
        return
    }
    pageSize = this.options[this.options.selectedIndex].value;
    pageNo=1;
    showList();
});


////审核状态
$('.verify>.btn-outline').on("click", function () {
    if ($(this).hasClass("total")) {
        check = -1;
    } else if ($(this).hasClass("signed")) {
        check = 1;
    } else if ($(this).hasClass("noSign")) {
        check = 0;
    }
    pageNo = 1;
    showList();
});


//创建页码信息
function createPageInfo(data) {
    $("#startRow").text(data.startRow);
    $("#endRow").text(data.endRow);
    $("#totalPage").text(data.total);
    var temp = '';
    for (var i = 1; i < data.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
        .val(data.pageNum)
        .change(function () {
            $("#pageInfor").blur(); 
            if (flag == 1) {
                return
            }
            pageNo = this.options[this.options.selectedIndex].value;
            showList();
        });
    $("#firstPage").click(function () {
        if (pageNo != 1) {
            if (flag == 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < data.pages) {
            if (flag == 1) {
                return
            }
            pageNo = data.pages;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < data.pages) {
            if (flag == 1) {
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag == 1) {
                return
            }
            pageNo--;
            showList();
        }
    });
}